/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Scene to display splash image + loading screen
 */

#include "../MainEngine.h"
#include "../Resources.h"
#include "SplashScene.h"
#include "SplashLayer.h"

using namespace cocos2d;

// total time to show this scene
const float TOTAL_WAIT_TIME = 1.0f;

// default constructor
SplashScene::SplashScene( MainEngine *mainEngine ) : mMaster( mainEngine ), mTimeLeft( TOTAL_WAIT_TIME ), mLoadingResources( false ), mLoadingComplete( false ) {
	initScene();
}

// default destructor
SplashScene::~SplashScene() {
	removeChild( mSplashLayer, true );
}

// initialize this scene
void SplashScene::initScene() {
	// create logo-layer
	mSplashLayer = new SplashLayer( this );
	addChild( mSplashLayer );
	// when the layer-object is no longer needed, it can be destroyed
	mSplashLayer->release();

	// this scene needs to be updated
	scheduleUpdate();
}

// keeps updating this scene
void SplashScene::update( ccTime dt ) {
	if( mLoadingResources == false ) {
		mLoadingResources = true;
		loadResources();
	}

	if( mLoadingComplete)
		mTimeLeft -= dt;

	// update layer
	mSplashLayer->update( dt );

	// when the scene is ready, notify master
	if( mTimeLeft <= 0.0f ) {
		mMaster->sceneFinishedExecution();
		mTimeLeft = 999.0f;
	}
}

// get master
MainEngine *SplashScene::getMaster() {
	return mMaster;
}

// loading callback
unsigned int resourcesLoaded = 0;
void SplashScene::loadingCallback( cocos2d::CCObject *pSender ) {
	++resourcesLoaded;
	if( resourcesLoaded >= 53 && mLoadingComplete == false )
		mLoadingComplete = true;
}

// loading function for the resources
void SplashScene::loadResources() {
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BOMB_MEDIUM ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BOMB_SMALLMINE_A ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BOMB_SMALLMINE_B ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BORDER_WOOD ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BORDER_WOOD_BROKEN ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_BORDER_WOOD_ICON ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BAR ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BAR_BROKEN ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BAR_PART_A ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BAR_PART_B ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BAR_PART_C ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_BROKEN ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_PART_A ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_STEEL_PART_B ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_WOOD ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_WOOD_BROKEN ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_WOOD_PART_A ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_ELEMENT_WOOD_PART_B ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_GROUND_CONCRETE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_GROUND_DIRT ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_GROUND_GRASS ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_BACKGROUND ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_BUTTON_BACKGROUND ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_EXIT ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_EXIT_PRESSED ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_HELP ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_HELP_PRESSED ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_ROTATE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_ROTATE_PRESSED ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_STATE_IMMOVABLE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_STATE_MOVABLE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_INTERFACE_STATE_NEUTRAL ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_CARTOONFIRE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_DUST ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_INVISIBLE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_SPARKLE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_SPLINTER ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::LOADING_ICON ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::LOADING_UNIT ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_BACKGROUND ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_BACKGROUND_NOTEBOARD ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_BUTTON_CLOSE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_BUTTON_CLOSE_SELECTED ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_LEVEL_BUTTON ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_LEVEL_BUTTON_SELECTED ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_LEVEL_LOCK ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_UNIT ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_UNIT_LOSE ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::MENU_UNIT_WIN ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_CLOUD1 ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_CLOUD2 ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
	cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync( getResource( Textures::GAME_MISC_CLOUD3 ), this, cocos2d::SEL_CallFuncO( &SplashScene::loadingCallback ) );
}

